/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.TourBookingBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.TourBooking;
import model.TourBookingDetails;

/**
 *
 * @author Nhang Con
 */
@ManagedBean
@RequestScoped
public class MngTourBookingBean {

    private List<TourBooking> listTourBooking;
    private TourBooking tourbooking;
    private String userNames;
    private String packageNames;
    private List<TourBookingDetails> listTourBookingDetail;
    private List<Integer> pageIndex;

    public List<Integer> getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(List<Integer> pageIndex) {
        this.pageIndex = pageIndex;
    }

    public List<TourBookingDetails> getListTourBookingDetail() {
        return listTourBookingDetail;
    }
    private TourBookingBusiness tbookbusiness;

    public List<TourBooking> getListTourBooking() {
        return listTourBooking;
    }

    public void setListTourBooking(List<TourBooking> listTourBooking) {
        this.listTourBooking = listTourBooking;
    }

    public String getPackageNames() {
        return packageNames;
    }

    public void setPackageNames(String packageNames) {
        this.packageNames = packageNames;
    }

    public TourBooking getTourbooking() {
        return tourbooking;
    }

    public void setTourbooking(TourBooking tourbooking) {
        this.tourbooking = tourbooking;
    }

    public String getUserNames() {
        return userNames;
    }

    public void setUserNames(String userNames) {
        this.userNames = userNames;
    }
    private int pageID;

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    /** Creates a new instance of MngTourBookingBean */
    public MngTourBookingBean() {

        tbookbusiness = new TourBookingBusiness();
        listTourBookingDetail = new ArrayList<TourBookingDetails>();
        TourBookingDetails tourBookingDetails;

        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID1").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID2").toString());
        }


        int index = tbookbusiness.getRowInListTour();
        pageIndex = new ArrayList<Integer>();

        if (index / 3 < 1) {
            pageIndex.add(1);
        } else {
            if (index % 3 == 0) {
                for (int i = 1; i <= index / 3; i++) {
                    pageIndex.add(i);
                }
            } else {
                for (int i = 1; i <= index / 3 + 1; i++) {
                    pageIndex.add(i);
                }
            }
        }
        listTourBooking = tbookbusiness.getListTourBooking(pageID);
        for (TourBooking tourBooking : listTourBooking) {
            tourBookingDetails = new TourBookingDetails();
            tourBookingDetails.setTourBooking(tourBooking);
            tourBookingDetails.setUsernames(tbookbusiness.getUserNameFromID(tourBooking.getCustomerID()));
            tourBookingDetails.setPackagenames(tbookbusiness.getPackageNameFromID(tourBooking.getPackageTourID()));
            listTourBookingDetail.add(tourBookingDetails);
        }

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID2", pageID);

    }

    public String updateStatusBooking(TourBooking tour) {

        if (tbookbusiness.updateStatusBooking(tour.getPackageTourID(), tour.getTourBookingID(), 0, tour.getCustomerID())) {

            return "managertourbooking.xhtml";

        }
        return "errol.xhtml";



    }
}
